﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include"vector.h"
using namespace std;



namespace bit
{
	template<class T>
	void Print(const vector<T>& v)
	{
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		v.push_back(7);
		v.push_back(8);
		v.push_back(9);
		v.push_back(10);

		v[0]++;
		Print(v);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	
	}

	void test2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		//v.push_back(5);

		auto it = v.begin() + 3;
		v.insert(v.begin(), 0);//触发重新分配内存，就空间释放
		v.insert(it, 30);//it指向旧空间，已失效
		
		Print(v);
	}

	void test3()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		Print(v);

		v.erase(v.begin());
		Print(v);

		auto it = v.begin() + 2;
		v.erase(it);//erase(it)一执行后就失效，不再保证指向合法内存，所以不能再访问it
		Print(v);
	}

	void test4()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);


		////删除所有的偶数
		//auto it = v.begin();
		//while (it != v.end())
		//{
		//	if (*it % 2 == 0)
		//	{
		//		v.erase(it);
		//	}

		//	++it;//it已经失效
		//}

		auto it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				it = v.erase(it);//it及时更新，不失效
			}
			else
			{
				++it;
			}
		}

		Print(v);
	}

	void test5()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		Print(v);

		v.resize(3);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		// v.resize(20);
		v.resize(20, 5);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test6()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		//拷贝构造
		vector<int> v2(v1);

		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;

		//initializer_list构造：vector<int> v3({ 10,20,30,40 });
		//initializer_list构造临时对象+临时对象拷贝构造v3（实际已优化）
		vector<int> v3 = { 10,20,30,40 };
		v1 = v3;//重载赋值运算符
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
		//10u能够匹配.h中的vector(size_t n, const T& val = T())（size_t 也是unsigned int）
		//否则优先匹配vector(InputIterator first, InputIterator last)
		vector<int> v4(10u, 1);
		//vector<int> v4(10, 1);
		for (auto e : v4)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<char> v5(10, 'x');
	}

	void test7()
	{
		vector<string> v1;
		v1.push_back("111111111111111111111111");
		v1.push_back("111111111111111111111111");
		v1.push_back("111111111111111111111111");
		v1.push_back("111111111111111111111111");
		v1.push_back("111111111111111111111111");
		//元素是内置类型时，auto浅拷贝；是资源分配类型时，auto调用string拷贝构造，深拷贝代价大
		//直接引用，避免深拷贝
		for (const auto& e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}


int main()
{
	//bit::test1();
	//bit::test2();
	//bit::test3();
	//bit::test4();
	//bit::test5();
	bit::test6();
	//bit::test7();
	return 0;
}